// pages/constmer/address-list/index.js
var api = require("../../../../api.js"),
  app = getApp();
Page({
  data: {
    list: [
      
    ],
  },
  navToEdit: function(e){
    
    wx.navigateTo({
      url: '/sd_liferuning/pages/constmer/address-insert/index?id=' + e.currentTarget.dataset.id,
    })
  },
  onShow:function(){
    app.pageOnLoad(this);
    var that = this
    var uid = wx.getStorageSync("uid");
    app.request({
      url: api.default.addslist,
      method:'get',
      data: {
        uid: uid,
        bid: wx.getStorageSync('bid')
      },
      success: function (data) {
        var result = data.data;
        for (var i=0;i<result.length;i++){
          if (result[i]['default']==1){
            result[i]['default'] ='success'
          }else{
            result[i]['default'] = 'circle'
          }
          result[i]['address'] = result[i]['province'] + result[i]['city'] + result[i]['area'] + result[i]['address'];
        }
        that.setData({
          list: data.data
        });
      }
    })
  },

  radio: function (e) {
    
    var that = this
    // //得到下标
    var index = e.currentTarget.dataset.index
    var newList = that.data.list

    app.request({
      url: api.default.defaultsite,
      method: 'get',
      data: {
        uid: wx.getStorageSync('uid'),
        uaid: index
      },
      success: function (data) {
        if (data.data.success==1){
          for (var i = 0; i < newList.length; i++) {
            if (newList[i]['uaid'] == index) {
              newList[i]['default'] = "success";
            } else {
              newList[i]['default'] = 'circle'
            }
          }
          that.setData({
            list: newList
          })
          wx.showToast({
            title: '设置成功',
            icon: 'success'
          })
          wx.navigateBack({ }) 
        }
      }
    })
  },
  //删除地址
  dellist: function (e) {

    var that = this
    //得到下标
    var uaid = e.currentTarget.dataset.id
    var index = e.currentTarget.dataset.index
    var newList = that.data.list
    // //得到选中状态
    wx.showModal({
      title: '提示',
      content: '是否删除该地址',
      success: function (res) {
        if (res.confirm) {
          app.request({
            url: api.default.delAddress,
            method: 'get',
            data: {
              uaid: uaid
            },
            success: function (data) {
              if (data.data.success == 1){
                newList.splice(index, 1)
                that.setData({
                  list: newList
                })
              }
              
            }
          })
        }
      }
    })
  },

})